package com.bw.module_cinema.model.repository

import com.bw.lib_net.BaseResponse
import com.bw.lib_storage.db.DBUtils
import com.bw.lib_storage.db.entity.VideoStarEntity
import com.bw.module_cinema.model.api.CinemaApi
import com.bw.module_cinema.model.entity.VideoByTypeIdEntity
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn
import javax.inject.Inject


class CinemaRepository @Inject constructor(private val cinemaApi: CinemaApi) {
    
    private val starDao by lazy {
        DBUtils.getDB().videoStarDao()
    }
    fun getCinemaType() = flow {
        emit(cinemaApi.getCinemaType())
    }.flowOn(Dispatchers.IO)

    fun getCinemaTypeByPid(pid: Int) = flow {
        emit(cinemaApi.getCinemaTypeByPid(pid))
    }.flowOn(Dispatchers.IO)

    fun getVideoByTypeId(
        page: Int,
        pagesize: Int,
        typeid: Int
    ):Flow<BaseResponse<List<VideoByTypeIdEntity>>> = flow {
        emit(cinemaApi.getVideoByTypeId(page, pagesize, typeid))
    }.flowOn(Dispatchers.IO)
    
    /**
     * 取消收藏视频
     */
    suspend fun cancelCollect(videoStar: VideoStarEntity) {
        kotlinx.coroutines.withContext(Dispatchers.IO) {
            starDao.deleteVideoStar(videoStar)
        }
    }
}